<?php
class Seo_Model_Filter extends Zend_Db_Table{
	
	protected $_name = 'seo_filter';
	protected $_primary ='id';
	protected $_ids;
	
	public function getItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		if($options['task'] == 'admin-info' || $options['task'] == 'admin-edit'){
			$select = $db	-> select()
							-> from('seo_filter AS f', array('*', "DATE_FORMAT(f.created,'%d/%m/%Y %H:%i:%s') AS created", "DATE_FORMAT(f.modified,'%d/%m/%Y %H:%i:%s') AS modified"))
							-> joinLeft('users AS u', 'f.created_by = u.id',array('u.user_name AS user_name'))
							-> where('f.id = ?', $arrParam['id'], INTEGER);
			$result = $db	-> fetchRow($select);
		}
		return $result;
	}
	
	public function listItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		
		if($options['task'] == 'admin-list'){
			
			$select = $db	->	select()
							->	from('seo_filter AS f')
							->	order('f.id ASC');
		
			$result = $db	-> fetchAll($select);
		}

		return $result;
		
	}

	public function saveItem($arrParam = null, $options = null){
	
		if($options['task'] == 'admin-add'){
			$row 				= $this->fetchNew();

			$info 					= new Zendvn_System_Info();
			$created_by 			= $info->getMemberInfo('id');

			$row->name 				= stripslashes($arrParam['name']);
			$row->status			= 1;
			$row->created 			= @date("Y-m-d H:i:s");
			$row->created_by		= $created_by;
			$row->modified 			= @date("Y-m-d H:i:s");
			$row->modified_by		= $created_by;
			$lastId = $row->save();
		}
	
		if($options['task'] == 'admin-edit'){
				
			$where 					= ' id=' . $arrParam['id'];
			$row 					= $this->fetchRow($where);

			$info 					= new Zendvn_System_Info();
			$modified_by 			= $info->getMemberInfo('id');
			
			$row->name 				= stripslashes($arrParam['name']);
			$row->modified 			= @date("Y-m-d H:i:s");
			$row->modified_by		= $modified_by;
			
			$row->save();
			
			$lastId = $arrParam['id'];
		}
		
		return $lastId;
	}

	public function deleteItem($arrParam = null, $options = null){
		$where = ' id=' . $arrParam['id'];
		$result = $this->delete($where);
	}
	
	public function changeStatus($arrParam = null, $options = null){
		if($arrParam['type'] == 1){
			$status = 1;
		}else{
			$status = 0;
		}

		$data = array('status' => $status);
		$where = 'id = ' . $arrParam['id'];
		$this->update($data, $where);
	}
}